package com.xiangban.yanglaodajie_platform.rabbit;

import com.xiangban.yanglaodajie_platform.dao.OrdersMapper;
import com.xiangban.yanglaodajie_platform.entity.Orders;
import com.xiangban.yanglaodajie_platform.entity.dto.OrdersDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

import static com.xiangban.yanglaodajie_platform.config.RabbitMqConfig.LOCK_ORDER_DEAD_QUEUE;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/6/23 13:16
 */
@Component
@RabbitListener(queues = "lock_order_dead_queue")
public class ConfirmOrderReceiver {

    @Autowired
    private OrdersMapper ordersMapper;


    private static Logger LOGGER = LoggerFactory.getLogger(CancelOrderReceiver.class);

    @RabbitHandler
    public void handle(String orderId){
        Orders orders = new Orders();
        orders.setOrderNumber(orderId);
        OrdersDto orderInfo = ordersMapper.orderInfoByOderNo(orders);
        if (orderInfo!=null){
            if (orderInfo.getOrderStatus().equals("1") && orderInfo.getTakesState()== -1){
                HashMap<String, Object> parameter = new HashMap<>();
                parameter.put("order_status","2");
                parameter.put("order_number",orderId);
                ordersMapper.updOrderStatus(parameter);
                LOGGER.info(orderId+"：订单已经完成");
            }
            if (orderInfo.getOrderStatus().equals("1") && orderInfo.getTakesState()== 3){
                HashMap<String, Object> parameter = new HashMap<>();
                parameter.put("order_status","2");
                parameter.put("order_number",orderId);
                ordersMapper.updOrderStatus(parameter);
                LOGGER.info(orderId+"：订单已经完成");
            }
        }
    }
}
